package com.ben.smm.view;

import android.app.PendingIntent;
import android.app.TabActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;

import com.ben.smm.broadcast.DeliveredReceiver;
import com.ben.smm.broadcast.SentReceiver;
import com.ben.smm.util.DESTool;
import com.ben.smm.util.SMMConstants;

/**
 * @author lixuanbin
 */
public class SMManagerActivity extends TabActivity {
	private TabHost myTabHost;
	private int sendCounter = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		myTabHost = this.getTabHost();
		LayoutInflater.from(this).inflate(R.layout.main,
				myTabHost.getTabContentView(), true);
		myTabHost.addTab(myTabHost
				.newTabSpec(getResources().getString(R.string.receive_tab))
				.setIndicator(getResources().getString(R.string.receive_tab),
						getResources().getDrawable(R.drawable.comment))
				.setContent(R.id.ssm_receivetab));
		myTabHost.addTab(myTabHost
				.newTabSpec(getResources().getString(R.string.send_tab))
				.setIndicator(getResources().getString(R.string.send_tab),
						getResources().getDrawable(R.drawable.mail))
				.setContent(R.id.ssm_sendtab));
		Button sendButton = (Button) findViewById(R.id.ssm_sendtab_submit_bt);
		sendButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText ssm_sendtab_contact_et = (EditText) findViewById(R.id.ssm_sendtab_contact_et);
				EditText ssm_sendtab_key_et = (EditText) findViewById(R.id.ssm_sendtab_key_et);
				EditText ssm_sendtab_content_et = (EditText) findViewById(R.id.ssm_sendtab_content_et);
				String phoneNumber = ssm_sendtab_contact_et.getText()
						.toString();
				String encrytionKey = ssm_sendtab_key_et.getText().toString();
				String content = ssm_sendtab_content_et.getText().toString();
				if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
					try {
						DESTool des = new DESTool(encrytionKey);
						String encryptedContent = "SM$" + des.encrypt(content)
								+ "SM#";
						v.setClickable(false);
						boolean isSent = sendSMS(phoneNumber, encryptedContent);
						if (isSent) {
							v.setClickable(true);
						}
					} catch (Exception e) {
						e.printStackTrace();
						Log.e("ssm", e.getMessage());
					}
				}
			}
		});
	}

	/**
	 * 发送一条短信
	 * 
	 * @param phoneNumber
	 * @param encryptedContent
	 * @param view
	 */
	private boolean sendSMS(String phoneNumber, String encryptedContent) {
		// PendingIntent sendIntent = PendingIntent.getActivity(this, 0,
		// new Intent(this, SMManagerActivity.class), 0);
		boolean isSent = false;
		Bundle bundle = new Bundle();
		bundle.putString("phone", phoneNumber);
		Intent sentIntent = new Intent(SMMConstants.SMS_SENT);
		sentIntent.putExtras(bundle);
		Intent deliveryIntent = new Intent(SMMConstants.SMS_DELIVERED);
		// deliveryIntent.putExtras(bundle);
		PendingIntent sentPendingIntent = PendingIntent.getBroadcast(
				getBaseContext(), 0, sentIntent,
				PendingIntent.FLAG_ONE_SHOT);
		PendingIntent deliveryPendingIntent = PendingIntent.getBroadcast(
				getBaseContext(), 0, deliveryIntent,
				PendingIntent.FLAG_ONE_SHOT);
		SmsManager smsManager = SmsManager.getDefault();
		try {
			smsManager.sendTextMessage(phoneNumber, null, encryptedContent,
					sentPendingIntent, deliveryPendingIntent);
			Toast.makeText(SMManagerActivity.this, "Message Sent.", 8000)
					.show();
			SentReceiver sentReceiver = new SentReceiver();
			registerReceiver(sentReceiver, new IntentFilter(
					SMMConstants.SMS_SENT));
			// SMManagerActivity.this.unregisterReceiver(sentReceiver);
			DeliveredReceiver deliveryReceiver = new DeliveredReceiver();
			registerReceiver(deliveryReceiver, new IntentFilter(
					SMMConstants.SMS_DELIVERED));
			// SMManagerActivity.this.unregisterReceiver(deliveryReceiver);
			isSent = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSent;
	}

}